package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.dao.OfflineCountRecordMapper;
import com.wsd.smartcarwasher.dao.StatisticDao;
import com.wsd.smartcarwasher.domain.OfflineCountRecord;
import com.wsd.smartcarwasher.domain.OfflineCountRecordExample;
import com.wsd.smartcarwasher.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OfflineCountRecordService {

    @Resource
    private OfflineCountRecordMapper offlineCountRecordMapper;
    @Resource
    private StatisticDao statisticDao;


    /**
     * 查询每天的洗车单数，再根据洗车单数减去当天的洗车订单数，得到当天的线下洗车单数
     * @return
     */
    public int getOfflineCountRecord(String sTime, Integer partnerId,Integer machineId)
    {
        Date date=DateUtil.strToDate(sTime);
        sTime=DateUtil.format(date,"YYYY-MM-dd");
//        System.out.println(sTime+"ssssss");
        String dTime=sTime+" 23:59:59";
        sTime=sTime+" 00:00:00";
        Map<String,Object> map=new HashMap<>();

        OfflineCountRecordExample  offlineCountRecordExample=new OfflineCountRecordExample();
        offlineCountRecordExample.createCriteria().andRecordtimeBetween(DateUtil.strToDate(sTime),DateUtil.strToDate(dTime));
        if(machineId!=null)
        {
            offlineCountRecordExample.createCriteria().andMachineidEqualTo(machineId.toString());
        }
        List<OfflineCountRecord> offlineCountRecordList=this.offlineCountRecordMapper.selectByExample(offlineCountRecordExample);
        //当天三种洗车数量
        if(!offlineCountRecordList.isEmpty())
        {
        int garment=offlineCountRecordList.get(0).getGarmentwashnum();//普洗
        int quick=offlineCountRecordList.get(0).getQuickwashnum();//快洗
        int waxing=offlineCountRecordList.get(0).getWaxingwashnum();//精洗
        //当天三种订单数量
        int garmentOrder=0;
        int quickOrder=0;
        int waxingOrder=0;

        if(!this.statisticDao.queryOnline(sTime,dTime,1,partnerId,machineId).isEmpty())
        {
            garmentOrder=Integer.parseInt(this.statisticDao.queryOnline(sTime,dTime,1,partnerId,machineId).get("count").toString()) ;
        }
        if(!this.statisticDao.queryOnline(sTime,dTime,0,partnerId,machineId).isEmpty())
        {
            quickOrder=Integer.parseInt(this.statisticDao.queryOnline(sTime,dTime,0,partnerId,machineId).get("count").toString());
        }
        if(!this.statisticDao.queryOnline(sTime,dTime,2,partnerId,machineId).isEmpty())
        {
            waxingOrder=Integer.parseInt(this.statisticDao.queryOnline(sTime,dTime,2,partnerId,machineId).get("count").toString());
        }
//        map.put("totalNum",offlineCountRecordList.get(0).getTatalnum());
//        map.put("offlineGar",garment-garmentOrder);
//        map.put("offlineQui",quick-quickOrder);
//        map.put("offlineWax",waxing-waxingOrder);
        map.put("onlineGar",garmentOrder);
        map.put("onlineQui",quickOrder);
        map.put("onlineWax",waxingOrder);

        return garment-garmentOrder+quick-quickOrder+waxing-waxingOrder;}
        else {
            return 0;
        }
    }

}
